<template>
	<div class="sys-org-container">
		<el-row :gutter="8" class="el-row">

			<div style="width: 100%;">
				<screening-Condition :inquireInfo="state.inquireInfo" @getChildren="getData">
					<el-button type="primary" icon="ele-Plus" @click="addDrawer = true" v-auth="'sysOrg:add'">
						新增
					</el-button>
				</screening-Condition>
			</div>
			<div class="container">
				<el-card class="table" shadow="hover">
					<el-table :data="state.Data" class="el-table" v-loading="state.loading" row-key="id"
						default-expand-all :tree-props="{ children: 'children', hasChildren: 'hasChildren' }" border
						highlight-current-row :header-cell-style="{
							background: 'var(--el-color-primary)',
							height: '55px', color: '#fff', border: '1px solid #fff', fontWeight: 'bold', fontSize: '13px'
						}" :cell-style="{ padding: '2px' }" max-width="440">
						<el-table-column type="index" label="序号" align="center"></el-table-column>
						<el-table-column prop="palletStatus" label="状态" header-align="center" show-overflow-tooltip
							align="center" width="70">
							<template #default="scope">
								<el-tag type="success" v-if="scope.row.palletStatus === 1">启用</el-tag>
								<el-tag type="danger" v-else>停用</el-tag>
							</template>
						</el-table-column>
						<el-table-column prop="palletNum" label="栈板编号" align="center" show-overflow-tooltip />
						<el-table-column prop="palletName" label="栈板名称" align="center" show-overflow-tooltip />
						<el-table-column prop="palletSpecs" label="规格型号" width="120" align="center"
							show-overflow-tooltip />
						<el-table-column prop="storage" label="库位" width="120" align="center" show-overflow-tooltip />

						<el-table-column prop="expirationDate" label="有效日期" align="center" show-overflow-tooltip />
						<el-table-column prop="palletRemake" label="备注" header-align="center" show-overflow-tooltip
							align="center" />
						<el-table-column prop="createUserName" label="新增人" align="center" header-align="center"
							show-overflow-tooltip width="70" />
						<el-table-column prop="CreateTime" label="新增日期" align="center" header-align="center"
							show-overflow-tooltip />

						<el-table-column label="操作" width="140" fixed="right" align="center" show-overflow-tooltip>
							<template #default="scope">
								<el-button size="small" text type="primary" @click="openEdit(scope.row)"> 编辑
								</el-button>
								<el-button size="small" text type="primary" @click="delOrg(scope.row)"
									v-auth="'pallet:delete'"> 删除 </el-button>
							</template>
						</el-table-column>
					</el-table>
					<el-pagination v-model:current-page="state.queryParams.page"
						v-model:page-size="state.queryParams.pageSize" :page-sizes="[10, 20, 30, 40]"
						layout="->,total, sizes, prev, pager, next, jumper" :total="state.total"
						@size-change="handleSizeChange" @current-change="handleCurrentChange" :small="true" />
				</el-card>
			</div>

		</el-row>

		<el-drawer v-model="addDrawer" :append-to-body="true" size="30%">
			<el-form :model="state.ruleForm" label-width="auto" class="fl" ref="ruleFormRef">
				<el-form-item label="编号" prop="palletNum"
					:rules="[{ required: true, message: '栈板编号不能为空', trigger: 'blur' }]">
					<el-input v-model="state.ruleForm.palletNum" placeholder="栈板编号" clearable class="input" />
				</el-form-item>
				<el-form-item label="名称" prop="palletName"
					:rules="[{ required: true, message: '栈板名称不能为空', trigger: 'blur' }]">
					<el-input v-model="state.ruleForm.palletName" placeholder="栈板名称" clearable class="input" />
				</el-form-item>
				<el-form-item label="型号" prop="palletSpecs"
					:rules="[{ required: true, message: '栈板型号不能为空', trigger: 'blur' }]">
					<el-select v-model="state.ruleForm.palletSpecs" filterable clearable class="w100">
						<el-option v-for="item in state.specifications" :key="item" :label="item.value"
							:value="item.value" />
					</el-select>
				</el-form-item>
				<el-form-item label="状态" prop="palletStatus"
					:rules="[{ required: true, message: '状态不能为空', trigger: 'blur' }]">
					<el-radio-group v-model="state.ruleForm.palletStatus" class="ml-4">
						<el-radio :label="1">启用</el-radio>
						<el-radio :label="0">停用</el-radio>
					</el-radio-group>
				</el-form-item>
				<el-form-item label="有效限期" prop="expirationDate"
					:rules="[{ required: true, message: '有效限期不能为空', trigger: 'blur' }]">
					<el-date-picker v-model="state.ruleForm.expirationDate" type="date" placeholder="请选择" />
				</el-form-item>
				<el-form-item label="备注">
					<el-input v-model="state.ruleForm.palletRemake" placeholder="请输入备注内容" clearable type="textarea"
						class="extra" />
				</el-form-item>
			</el-form>
			<div style="width: 100%;display: flex;justify-content: center;margin-top:60px">
				<el-button @click="submit" type="primary" round style="width:80px;height:30px">确认</el-button>
				<el-button @click="addDrawer = false" round style="width:80px;height:30px">取消</el-button>
			</div>
		</el-drawer>
		<el-drawer v-model="editDrawer" :append-to-body="true" size="30%" @close="handleEditCancel">
			<el-form :model="state.ruleForm2" ref="editRuleFormRef" label-width="auto" class="fl">
				<el-form-item label="编号" prop="palletNum"
					:rules="[{ required: true, message: '栈板编号不能为空', trigger: 'blur' }]">
					<el-input v-model="state.ruleForm2.palletNum" placeholder="栈板编号" clearable class="input" disabled />
				</el-form-item>
				<el-form-item label="名称" prop="palletName"
					:rules="[{ required: true, message: '栈板名称不能为空', trigger: 'blur' }]">
					<el-input v-model="state.ruleForm2.palletName" placeholder="栈板名称" clearable class="input" />
				</el-form-item>
				<el-form-item label="型号" prop="palletSpecs"
					:rules="[{ required: true, message: '栈板型号不能为空', trigger: 'blur' }]">
					<el-select v-model="state.ruleForm2.palletSpecs" filterable clearable class="w100">
						<el-option v-for="item in state.specifications" :key="item" :label="item.value"
							:value="item.value" />
					</el-select>
				</el-form-item>
				<el-form-item label="状态" prop="palletStatus"
					:rules="[{ required: true, message: '状态不能为空', trigger: 'blur' }]">
					<el-radio-group v-model="state.ruleForm2.palletStatus" class="ml-4">
						<el-radio :label="1">启用</el-radio>
						<el-radio :label="0">停用</el-radio>
					</el-radio-group>
				</el-form-item>
				<el-form-item label="有效限期" prop="expirationDate"
					:rules="[{ required: true, message: '有效限期不能为空', trigger: 'blur' }]">
					<el-date-picker v-model="state.ruleForm2.expirationDate" type="date" placeholder="请选择" />
				</el-form-item>
				<el-form-item label="备注">
					<el-input v-model="state.ruleForm2.palletRemake" placeholder="请输入备注内容" clearable type="textarea"
						class="extra" />
				</el-form-item>
			</el-form>
			<div style="width: 100%;display: flex;justify-content: center;margin-top:60px">
				<el-button @click="submitEdit" type="primary" round style="width:80px;height:30px">确认</el-button>
				<el-button @click="handleEditCancel" round style="width:80px;height:30px">取消</el-button>
			</div>
		</el-drawer>
	</div>
</template>

<script lang="ts" setup name="sysOrg">
import { onMounted, reactive, ref } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import { getAPI } from '/@/utils/axios-utils';
import { SysDictDataApi } from '/@/api-services/api';
import { Pallet } from '/@/api-services/models';
import { dateFormat, dateShortFormat } from '/@/utils/date'

import { storeToRefs } from "pinia";
import { WmsApi } from "/@/api/wh";
import screeningCondition from '/@/views/tools/screeningCondition.vue'
const ruleFormRef = ref();
const editRuleFormRef = ref();
const addDrawer = ref(false);
const editDrawer = ref(false);
const state = reactive({
	loading: false,
	Data: [], //机构列表数据
	queryParams: {
		pageSize: 20,
		page: 1,
		expirationDateRange: [],
		palletNum: '',
		// palletName: '',
		palletSpecs: ''
	},
	editOrgTitle: '',
	specifications: [],
	advanced: false,
	color: '#de2910',
	size: '14px',
	ruleForm: {},
	ruleForm2: {},
	total: 0,
	value: {},
	inquireInfo: [
		{
			label: "栈板编号",
			value: "",
			key: "palletNum",
			options: [],
			type: 'input'
		},

		{
			label: "规格型号",
			value: "",
			key: "palletSpecs",
			options: [],
			type: 'input'
		},
		{
			label: "有效日期",
			value: "",
			key: "expirationDateRange",
			options: [],
			type: 'date'
		}
	],
});

onMounted(async () => {
	let arr = ['name', 'age', 'name'];
	let res = arr.reduce((pre:any, cur:any) => {
		if (!pre[cur]) {
			console.log(pre[cur]);
			pre[cur] = 1
		}
		else { pre[cur]++ }
	});
	console.log(res);
	let resDicData = await getAPI(SysDictDataApi).apiSysDictDataDataListCodeGet('code_gen_Pallet_type');
	state.specifications = resDicData.data.result;
	getData(state.queryParams);
});
const getData = async (value) => {
	state.value = value;

	if (value.expirationDateRange) {
		var expirationDateRange = [value.expirationDateRange[0], value.expirationDateRange[1]]

	} else {
		var expirationDateRange = []
	}
	var params = {
		page: state.queryParams.page,
		pageSize: state.queryParams.pageSize,
		palletNum: value.palletNum,
		expirationDateRange: expirationDateRange,

		palletSpecs: value.palletSpecs
	}
	await WmsApi().PalletPagePost(params)
		.then(function (res) {
			state.Data = res.data.result.items;
			state.total = res.data.result.total;
		})
		.catch(function (error) {
			console.log(error);
		});
}
const submit = () => {
	console.log(ruleFormRef?.value);
	ruleFormRef?.value.validate(async (valid, object) => {
		if (!valid) return false;
		await WmsApi().PalletPageAdd(state.ruleForm)
			.then(function (response) {
				ElMessage.success('新增成功')

				getData(state.queryParams);
			})
			.catch(function (error) {
				console.log(error);
			});
		addDrawer.value = false;
	});
}
const submitEdit = () => {
	editRuleFormRef?.value.validate(async (valid, object) => {
		if (!valid) return false;
		await WmsApi().PalletPagePut(state.ruleForm2)

			.then(function (response) {
				ElMessage.success('修改成功')
				getData(state.queryParams);
			})
			.catch(function (error) {
				console.log(error);
			});
		editDrawer.value = false;

	});
}

const openEdit = (row) => {
	editDrawer.value = true;
	state.ruleForm2 = Object.assign({}, row)

};
const toggleAdvanced = () => {
	state.advanced = !state.advanced;
	console.log(state.advanced);

}

const delOrg = (row) => {
	console.log(row);
	ElMessageBox.confirm(`确定删除该区域：【${row.palletName}】?`, '提示', {
		confirmButtonText: '确定',
		cancelButtonText: '取消',
		type: 'warning',
	})
		.then(async () => {
			await WmsApi().PalletPageDelete({ id: row.id })
				.then(function (res) {
					ElMessage.success('删除成功');
					getData(state.queryParams);
				})
		})
		.catch(() => {
			ElMessage({
				type: 'info',
				message: 'Delete canceled',
			})
		});
};
const handleSizeChange = (val) => {
	state.queryParams.pageSize = val;
	state.queryParams.palletNum = state.value.palletNum;
	state.queryParams.palletName = state.value.palletName;
	state.queryParams.palletSpecs = state.value.palletSpecs;
	state.queryParams.expirationDate = state.value.expirationDate;


	getData(state.queryParams);
};
const handleEditCancel = () => {
	editDrawer.value = false;

}
const handleCurrentChange = (newVal) => {
	state.queryParams.page = newVal;
	state.queryParams.palletNum = state.value.palletNum;
	state.queryParams.productionDate = state.value.productionDate;
	state.queryParams.palletSpecs = state.value.palletSpecs;
	state.queryParams.expirationDate = state.value.expirationDate;

	getData(state.queryParams);
};

</script>
<style lang="scss" scoped>
.fl {
	width: 60%;
	margin: 20px 80px;
}
</style>
